#include "StdAfx.h"
#include ".\commandbase.h"

namespace GPCommon
{

CommandBase::CommandBase(DWORD accelerator) :
  accelerator_(accelerator)
{
}

CommandBase::~CommandBase()
{
}

HRESULT WINAPI CommandBase::ICommandAcceleratorQIFunc(void* pv, REFIID riid, LPVOID* ppv, DWORD dw)
{
	CommandBase *pThis = static_cast<CommandBase *>(pv);
	*ppv = NULL;
	if (pThis->accelerator_)
	{
		*ppv = static_cast<GrymCore::ICommandAccelerator *>(pThis);
		pThis->AddRef();
		return S_OK;
	}
	return E_NOINTERFACE;
}

STDMETHODIMP CommandBase::get_Accelerator(LONG *pVal)
{
	if (!pVal)
	{
		return E_POINTER;
	}
	*pVal = accelerator_;
	return S_OK;
}

VisibleCommandBase::VisibleCommandBase(const _bstr_t &placement_code, const _bstr_t &tag, UINT rcid_caption, 
	UINT rcid_description, IUnknown *icon, DWORD accelerator) :
	CommandBase(accelerator),
	placement_code_(placement_code),
	tag_(tag),
	icon_(icon)
{
	caption_ = GPCommon::LoadResourceBSTR(rcid_caption);
	description_ = GPCommon::LoadResourceBSTR(rcid_description);
}

VisibleCommandBase::~VisibleCommandBase()
{
}

void VisibleCommandBase::SetAppearance(const _bstr_t &tag, 
									   const _bstr_t &caption, 
									   const _bstr_t &description, 
									   IUnknown *icon)
{
    tag_ = tag;
    caption_ = caption;
    description_ = description;
    icon_ = icon;
}
 
void VisibleCommandBase::SetPlacement(const _bstr_t &placement_code)
{
    placement_code_ = placement_code;
}

void VisibleCommandBase::SetAccelerator(DWORD accelerator) 
{
    accelerator_ = accelerator; 
}


STDMETHODIMP VisibleCommandBase::get_PlacementCode(BSTR *pVal)
{
	if (!pVal)
	{
		return E_POINTER;
	}
	*pVal = placement_code_.copy();
	return S_OK;
}

STDMETHODIMP VisibleCommandBase::get_Tag(BSTR *pVal)
{
	if(!pVal)
	{
		return E_POINTER;
	}
	*pVal = tag_.copy();
	return S_OK;
}

STDMETHODIMP VisibleCommandBase::get_Caption(BSTR *pVal)
{
	if(!pVal)
	{
		return E_POINTER;
	}
	*pVal = caption_.copy();
	return S_OK;
}

STDMETHODIMP VisibleCommandBase::get_Description(BSTR *pVal)
{
	if(!pVal)
	{
		return E_POINTER;
	}
	*pVal = description_.copy();
	return S_OK;
}

STDMETHODIMP VisibleCommandBase::get_Icon(IUnknown ** pVal)
{
	if(!pVal)
	{
		return E_POINTER;
	}
	*pVal = IUnknownPtr(icon_).Detach();
	return S_OK;
}

VisibleStateCommandBase::VisibleStateCommandBase(const _bstr_t &placement_code, const _bstr_t &tag, UINT rcid_caption, 
	UINT rcid_description, IUnknown *icon, DWORD accelerator) :
VisibleCommandBase(placement_code, tag, rcid_caption, rcid_description, icon, accelerator)
{
}

VisibleStateCommandBase::~VisibleStateCommandBase()
{
}

STDMETHODIMP VisibleStateCommandBase::get_Available(VARIANT_BOOL *pVal)
{
	if (!pVal)
	{
		return E_POINTER;
	}
	*pVal = VARIANT_TRUE;
	return S_OK;
}

STDMETHODIMP VisibleStateCommandBase::get_Enabled(VARIANT_BOOL *pVal)
{
	if (!pVal)
	{
		return E_POINTER;
	}
	*pVal = VARIANT_TRUE;
	return S_OK;
}

STDMETHODIMP VisibleStateCommandBase::get_Checked(VARIANT_BOOL *pVal)
{
	if (!pVal)
	{
		return E_POINTER;
	}
	*pVal = VARIANT_FALSE;
	return S_OK;
}

} // namespace GPCommon
